from collections import namedtuple


SB = namedtuple("SB", ["price", "number", "total", "cost", "buy"])


def r(a):
    return round(a, 2)


class Solution:

    def __init__(self, basic, money) -> None:
        self.SBS = []
        self.basic = basic
        self.money = money
        self.price = 0
        self.tot = 0
        self.prices = []

    def buy(self, sb: SB):
        self.basic += sb.number
        self.money += (
            sb.total
            if sb.total > 0
            else (
                self.count_tot(sb.price, sb.number)
                + self.count_fee(self.count_tot(sb.price, sb.number), sb)
            )
        )
        self.SBS.append(sb)
        self.price = sb.price
        # self.print_basic()

    def sell(self, sb: SB):
        self.basic -= sb.number
        self.money -= (
            sb.total
            if sb.total > 0
            else (
                self.count_tot(sb.price, sb.number)
                - self.count_fee(self.count_tot(sb.price, sb.number), sb)
            )
        )
        self.SBS.append(sb)
        self.price = sb.price
        # self.print_basic()

    def print_basic(self):
        fen = r(self.basic) * 1.5
        print(
            r(self.basic),
            r(self.money),
            # r(self.money / self.basic),
            fen,
            r(fen / 365),
            r(self.basic * self.price),
        )

    def count_tot(self, price, number):
        return r(price * number)

    def count_fee(self, tot, sb: SB):
        # 万分之一 + 印花税
        return r(tot * 0.0001) + r(0 if sb.buy else tot * 0.5 / 1000)

    def show(self):
        sm = 0
        smz = 0
        # print("%7s %10s %7s %6s %5s" % ("单价", "数量", "金额", "费用", "总额"))
        for sb in self.SBS:
            tot = self.count_tot(sb.price, sb.number)
            fee = self.count_fee(tot, sb)
            if sb.buy:
                sm -= tot + fee
                smz -= sb.total
            else:
                sm += tot - fee
                smz += (
                    sb.total
                    if sb.total > 0
                    else (
                        self.count_tot(sb.price, sb.number)
                        - self.count_fee(self.count_tot(sb.price, sb.number), sb)
                    )
                )
            # print(
            #     "{:10.2f} {:10d} {:10.2f} {:8.2f} {:10.2f} {:10.2f}".format(
            #         sb.price,
            #         sb.number,
            #         tot,
            #         fee,
            #         sb.total,
            #         sb.cost,
            #     )
            # )
            self.prices.append(sb.price)
            if not sb.buy:
                self.tot += sm
                print(
                    "%9.2f %10.2f %10.2f %10.2f"
                    % (r(self.prices[0]), r(self.prices[1]), r(sm), r(smz))
                )
                self.prices = []
                sm = 0
                smz = 0
